#pragma once

#include <iostream>
#include <memory>
#include <sys/epoll.h>
#include "Socket.hpp"
#include "Epoller.hpp"
#include "Log.hpp"
#include "nocopy.hpp"

uint32_t EVENT_IN = (EPOLLIN);
uint32_t EVENT_OUT = (EPOLLOUT);

class EpollServer : public nocopy
{
    const static int num = 64;

public:
    EpollServer(uint16_t port)
        : _port(port),
          _listensock_ptr(new Sock()),
          _epoller_ptr(new Epoller())
    {
    }
    void Init()
    {
        _listensock_ptr->Socket();
        _listensock_ptr->Bind(_port);
        _listensock_ptr->Listen();

        lg(Info, "create listen socket success: %d\n", _listensock_ptr->Fd());
    }

    void Acceptor()
    {
        std::string clientip;
        uint16_t clientport;
        int sock = _listensock_ptr->Accept(&clientip, &clientport);
        if (sock > 0)
        {
            // 不能直接读取,建立好连接不一定就有数据
            _epoller_ptr->EpollerUpdate(EPOLL_CTL_ADD, sock, EVENT_IN);
            lg(Info,"get a new link,client info@ %s:%d",clientip.c_str(),clientport);
        }
        else
        {
            return;
        }
    }

    void Recver(int fd)
    {
        char buffer[1024];
        ssize_t n = read(fd,buffer,sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout<<"get a message"<<buffer<<std::endl;

            //write
            std::string echo_str = "Server echo# ";
            echo_str += buffer;
            write(fd,echo_str.c_str(),echo_str.size());
        }
        else if(n == 0)
        {
            lg(Info,"client quit,close fd : %d",fd);
            
            //移除事件关心,应该先移除(文件描述符还合法时)再关闭文件描述符
            _epoller_ptr->EpollerUpdate(EPOLL_CTL_DEL,fd,0);
            close(fd);
        }
        else
        {
            lg(Warning,"recv error,fd : %d",fd);
            close(fd);
            
        }
    }


    void Dispatcher(struct epoll_event revs[], int num)
    {
        for (int i = 0; i < num; i++)
        {
            uint32_t events = revs[i].events;
            int fd = revs[i].data.fd;
            if (events & EVENT_IN)
            {
                if (fd == _listensock_ptr->Fd())
                {
                    // listen套接字获取连接
                    Acceptor();
                }
                else
                {
                    //其他文件描述符就绪
                    Recver(fd);
                }
            }
            else if (events & EVENT_OUT)
            {
            }
            else
            {
            }
        }
    }

    void Start()
    {
        // 将listensock添加到epoll中 ->epoll和他关心的事件, 添加到内核epoll模型tb_tree中
        _epoller_ptr->EpollerUpdate(EPOLL_CTL_ADD, _listensock_ptr->Fd(), EVENT_IN);

        struct epoll_event revs[num];
        while (true)
        {
            int n = _epoller_ptr->EpollerWait(revs, num);
            if (n > 0)
            {
                // 有事件就绪
                lg(Debug, "event happened,fd is: %d", revs[0].data.fd);
                Dispatcher(revs, n);
            }
            else if (n == 0)
            {
                lg(Info, "timeout...");
            }
            else
            {
                lg(Error, "epoll wait error");
            }
        }
    }
    ~EpollServer()
    {
        _listensock_ptr->Close();
    }

private:
    std::shared_ptr<Sock> _listensock_ptr;
    std::shared_ptr<Epoller> _epoller_ptr;
    uint16_t _port;
};